package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CustomException;
import com.common.R;
import com.entity.Category;
import com.entity.Dish;
import com.entity.Setmeal;
import com.mapper.CategoryMapper;
import com.service.CategoryService;
import com.service.DishService;
import com.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Override
    public void remove(Long id) {
        //查找要删除的分类是否套餐有关联
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,id);
        long count1 = setmealService.count(lqw);
        if (count1>0){
             throw new CustomException("该类有关联，删个der");
        }
        //查找要删除的分类是否菜品有关联
        LambdaQueryWrapper<Dish> lqw2=new LambdaQueryWrapper<>();
        lqw2.eq(Dish::getCategoryId,id);
        long count2 = dishService.count(lqw2);
        if (count2>0){
            throw new CustomException("该类有关联，删个der");
        }
        super.removeById(id);
    }
}
